package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Categories;
import com.blb.entity.User;
import com.blb.service.ICategoriesService;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-20
 */
@Controller
@RequestMapping("/categories")
public class CategoriesController {
    @Resource
    private ICategoriesService categoriesService;
    /*显示页面*/
    @RequiresPermissions("addtypesPage")
    @RequestMapping("Categories")
    public String getCategories(){
        return "/commodity/addtypes.html";
    }

    /**
     * 获取树的样式
     * @param model
     * @param companyId
     * @return
     */
    @RequiresPermissions(value = {"apply", "audit","supplier_permission"}, logical = Logical.OR)
    @ResponseBody
    @RequestMapping("getCategories")
    public List<Categories> getMenu(Model model, String companyId){
        return categoriesService.list(new QueryWrapper<Categories>().eq("company_id",companyId));
    }
    @ResponseBody
    @RequestMapping("getCategoriess")
    public List<Categories> getCategoriess(){
        User user = ShiroUtils.getUser();
        return categoriesService.list(new QueryWrapper<Categories>().eq("company_id",user.getCompanyId()));
    }
    @ResponseBody
    @RequestMapping("getById")
    public Categories getById(String id){
        Categories categories = categoriesService.getOne(new QueryWrapper<Categories>().eq("id", id));
        System.out.println(categories);
        return categories;
    }
    @ResponseBody
    @RequestMapping("addCategories")
    public String addCategories(Categories categories){
        User user = ShiroUtils.getUser();
        categories.setMIntroduction(categories.getMIntroduction().replace("\"",""));
        String cid = UUID.randomUUID().toString().replace("-","");
        categories.setPId(categories.getId());
        categories.setId(cid);
        categories.setUserId(user.getId());
        categories.setCompanyId(user.getCompanyId());
        System.out.println(categories);
        categoriesService.addCategories(categories);
        return "";
    }
    @RequestMapping("updateCategories")
    public String updateCategories(Categories categories){
        System.out.println(categories);
        categoriesService.updateCategories(categories);
        return "commodity/addtypes.html";
    }
    @ResponseBody
    @RequestMapping("deleteCategories")
    public String deleteCategories(String id){
        List<String> ids1 = categoriesService.findId(id);
        if(ids1.isEmpty()){
            categoriesService.deleteCategories(id);
        }else{
            categoriesService.deleteCategories(id);
            for(String d1:ids1){
                categoriesService.deleteCategories(d1);
                List<String> ids2 = categoriesService.findId(d1);
                for(String d2:ids2){
                    categoriesService.deleteCategories(d2);
                }
            }
        }
        return "";
    }
}

